#include "dynamicArray.h"


/* 动态数组（Dynamic Array）是指动态分配的、可以根据需求动态增长占用内存的数组。
 * 为了实现一个动态数组类的封装，我们需要考虑几个问题：
 * new/delete的使用、内存分配策略、类的四大函数（构造函数、拷贝构造函数、拷贝赋值运算符、析构函数）、运算符的重载。*/
void dynamicArray::init()
{
    size = 0;
    max = 1;
    data =  new double[max];
}

void dynamicArray::free()   // 释放动态内存
{
    delete [] data;
}

bool dynamicArray::InvalidateIndex(int index){
    if(index >= 0 && index < size)
        return false;
    else {
        return true;
    }
}

// 默认构造函数
dynamicArray::dynamicArray()
{
    init();
}

// 构造函数
dynamicArray::dynamicArray(int nSize, double dValue){

    if(nSize == 0)
    {
        init();
    }
    else {
        size = nSize;
        max = nSize;
        data = new double[nSize];
        for (int i = 0; i < nSize; i++) {
            data[i] = dValue;
        }
    }
}

// 拷贝构造函数
dynamicArray::dynamicArray(const dynamicArray &arr)
{
    size = arr.size;
    max = arr.max;
    data = new double[max];
    memcpy(data, arr.data, size*sizeof (double));
}


// 拷贝赋值运算符
dynamicArray &dynamicArray::operator=(const dynamicArray &arr){
    if(this == &arr)
        return *this;
    size = arr.size;
    max = arr.max;

    double *temp = new double[max];
    memcpy(temp, arr.data , size * sizeof (double));

    delete [] data;
    data = temp;

    return *this;
}

// 析构函数
dynamicArray::~dynamicArray(){
    free();
}

// 打印数组
void dynamicArray::print(){
    if(size == 0)
    {
        std::cout << "Error: The empty array can't be Printed." << std::endl;
        exit(0);
    }
    else {
        for (int i = 0; i < size; i++) {
            std::cout << data[i] << "";
            std::cout << std::endl;
        }
    }
}

// 获取数组大小
int dynamicArray::getSize(){
    return size;
}

// 重置数组大小
void dynamicArray::setSize(int nSize){
    if(nSize < size)    /*截断*/
    {
        for (int i = nSize; i < size; ++i) {
            data[i] = 0;
        }
    }
    if(size <= nSize && nSize <= max)   /*新增元素置0*/
    {
        for(int i = size; i < nSize; ++i)
        {
            data[i] = 0;
        }
    }

    if(nSize > max) /*需要重新分配空间*/
    {
        max = nSize;
        double *temp = new double[max];
        memcpy(temp, data, size * sizeof (double));

        for(int i = size; i < nSize; ++i)
        {
            temp[i] = 0;
        }
        delete[] data;
        data = temp;

    }
    size = nSize;   /*设置数组大小*/
}

// 获取指定位置元素
double dynamicArray::getPositionElement(int index){
    if(InvalidateIndex(index))
    {
        cout << "Error: the index of getPositionElement is invalid!" << endl;
        exit(0);
    }

    return data[index];
}

// 设置指定位置元素的值
void dynamicArray::setPositionElement(int index, double dValue){
    if(InvalidateIndex(index))
    {
        cout << "ErrorL the index of setPositionElement is invalid!" << endl;
        exit(0);
    }
    else {
        data[index] = dValue;
    }
}

// 追加一个新元素到数组末尾
void dynamicArray::pushBack(double dValue){
    if(size < max)
    {
        data[size] = dValue;
    }
    else {
        max = max * 2;
        double *temp = new double[max];
        memcpy(temp, data, size*sizeof (double));
        delete [] data;
        data = temp;
        data[size] = dValue;
    }
    ++size;
}

// 从数组中删除一个元素 O(n)
void dynamicArray::deletePositionElement(int index){
    if(InvalidateIndex(index))
    {
        cout << "ErrorL the index of deletePositionElement is invalid!" << endl;
        exit(0);
    }
    else {
        for (int i = index; i < size; i++) {
            data[i] = data[i+1];
        }
        data[size - 1] = 0;
        --size;
    }
}

// 插入一个新元素到指定位置
void dynamicArray::insertPositionElement(int index, double dValue){
    if(index < 0 || index > size)
    {
        cout << "ErrorL the index of insertPositionElement is invalid!" << endl;
        exit(0);
    }

    if(size < max)  /* 未满，插入 */
    {
        for (int i = index; i < size; i++) {
            data[i+1] = data[i];
        }
        data[index] = dValue;
    }
    else {       /* 重新分配空间 */
        max = max * 2;
        double *temp = new double[max];
        memcpy(temp, data, size*sizeof (double));
        delete [] data;
        data = temp;
        for (int i = size - 1; i >=index; --i) {
            data[i+1] = data[i];
        }
        data[size] = dValue;
    }
    ++size;/*数组大小加1*/
}

// 重载下标运算符[]
double dynamicArray::operator[](int index) const{
    if(index < 0 || index > size)
    {
        cout << "ErrorL the index of insertPositionElement is invalid!" << endl;
        exit(0);
    }

    return data[index];
}






